const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/mytest',{useUnifiedTopology: true,useNewUrlParser: true})
  .then(()=> console.log('链接成功'))
  .catch(err => console.log(err,'链接失败'))

const userSchema = new mongoose.Schema({
  name:{
    type: String,
    required: true
  }
})
const postSchema = new mongoose.Schema({
  title:{
    type: String
  },
  author: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User'
  }
})


const User = mongoose.model('User',userSchema)
const Post = mongoose.model('Post',postSchema)


// User.create({name:'luogege'}).then(res => {
//   console.log(res);
// })

// Post.create({title: '123',author:'6030d08c7c3b481b6c48b4b9'}).then(res => {
//   console.log(res);
// })
Post.find().populate('author').then(res=>{
  console.log(res);
})